﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using DLLPC.Utilities;
using PCBase.UIControls;

namespace COER
{
    public partial class IOView : Form
    {
        public IOView()
        {
            InitializeComponent();
            dataGridView_DI.DoubleBufferedDataGirdView(true);
            dataGridView_DO.DoubleBufferedDataGirdView(true);
        }

        MotionHelper myIO;

        public void ShowForm(MotionHelper vIO)
        {
            myIO = vIO;
            //Init();
            timer1.Start();
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private List<LoadFileHelper.DataItem> XDataItems = new List<LoadFileHelper.DataItem>();
        private List<LoadFileHelper.DataItem> YDataItems = new List<LoadFileHelper.DataItem>();
        private void IOView_Load(object sender, EventArgs e)
        {
            YDataItems = LoadFileHelper.LoadExcelFile("IOBlock.xlsx", "DO");
            dataGridView_DO.Rows.Clear();
            for (int i = 0; i < myIO.DO.GetCount(); i++)
            {
                DataGridViewRow tRow = new DataGridViewRow();
                int tIdx = dataGridView_DO.Rows.Add(tRow);

                if (i < YDataItems.Count)
                {
                    dataGridView_DO.Rows[tIdx].Cells[0].Value = YDataItems[i].Name.ToUpper();
                    dataGridView_DO.Rows[tIdx].Cells[1].Value = myIO.DO.GetValIdx(i).ToString();
                    dataGridView_DO.Rows[tIdx].Cells[3].Value = "修改";
                    dataGridView_DO.Rows[tIdx].Cells[4].Value = YDataItems[i].Comment;
                    dataGridView_DO.Rows[tIdx].Cells[4].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                }
            }

            XDataItems = LoadFileHelper.LoadExcelFile("IOBlock.xlsx", "DI");
            dataGridView_DI.Rows.Clear();
            for (int i = 0; i < myIO.DI.GetCount(); i++)
            {
                DataGridViewRow tRow = new DataGridViewRow();
                int tIdx = dataGridView_DI.Rows.Add(tRow);

                if (i < XDataItems.Count)
                {
                    dataGridView_DI.Rows[tIdx].Cells[0].Value = XDataItems[i].Name.ToUpper();
                    dataGridView_DI.Rows[tIdx].Cells[1].Value = myIO.DI.GetValIdx(i).ToString();
                    dataGridView_DI.Rows[tIdx].Cells[2].Value = XDataItems[i].Comment;
                    dataGridView_DI.Rows[tIdx].Cells[2].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                }
            }
        }

        private void IOStatus_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            this.Visible = false;
            e.Cancel = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false || this.WindowState == FormWindowState.Minimized) return;

            for (int i = 0; i < myIO.DO.GetCount(); i++)
            {
                dataGridView_DO.Rows[i].Cells[1].Value = myIO.DO.GetValIdx(i).ToString();
                dataGridView_DO.Rows[i].Cells[1].DataGridView.BackgroundColor = (myIO.DO.GetValIdx(i) == 1) ? Color.Green : Color.White;
            }

            for (int i = 0; i < myIO.DI.GetCount(); i++)
            {
                dataGridView_DI.Rows[i].Cells[1].Value = myIO.DI.GetValIdx(i).ToString();
                dataGridView_DI.Rows[i].Cells[1].DataGridView.BackgroundColor = (myIO.DI.GetValIdx(i) == 1) ? Color.Green : Color.White;
            }
        }

        public void LoadIOMapFile(string IOMapFile, string SheetName)
        {
            LoadFileHelper.LoadExcelFile(IOMapFile, SheetName);
            #region
            /*
            string filePath = Application.StartupPath + "\\SystemFile\\" + IOMapFile;
            if (System.IO.Directory.Exists(Application.StartupPath + "\\SystemFile") == false)
            {
                System.IO.Directory.CreateDirectory(Application.StartupPath + "\\SystemFile");
            }
            if (System.IO.File.Exists(filePath) == false)
            {
                MessageBox.Show(string.Format("SystemFile\\{0} 文件不存在", IOMapFile));
                return;
            }

            // EPPlus 序号从 1开始
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                ExcelPackage package = new ExcelPackage(fs);
                try
                {
                    XDataItems.Clear();
                    YDataItems.Clear();
                    ExcelWorksheet sheet1 = package.Workbook.Worksheets[SheetName];
                    for (int i = 1; i <= 512; i++)
                    {
                        IODataItem itemX = new IODataItem();
                        itemX.ItemName = sheet1.Cells[i + 1, 1].Value == null ? "" : sheet1.Cells[i + 1, 1].Value.ToString();
                        itemX.Comment = sheet1.Cells[i + 1, 2].Value == null ? "" : sheet1.Cells[i + 1, 2].Value.ToString();
                        XDataItems.Add(itemX);
                        IODataItem itemY = new IODataItem();
                        itemY.ItemName = sheet1.Cells[i + 1, 5].Value == null ? "" : sheet1.Cells[i + 1, 5].Value.ToString();
                        itemY.Comment = sheet1.Cells[i + 1, 6].Value == null ? "" : sheet1.Cells[i + 1, 6].Value.ToString();
                        YDataItems.Add(itemY);
                    }
                    package.Stream.Close();
                    package.Dispose();
                    fs.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("SystemFile\\{0} 文件读取失败" + Environment.NewLine + ex.Message, IOMapFile));
                }
            }
            */
            #endregion
        }

        #region Y点强制ON
        //private void dataGridView_Local_CellContentClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.ColumnIndex != 3) return;

        //    int tRowIdx = e.RowIndex;
        //    if (tRowIdx < 0) return;
        //    if (dataGridView_Local.Rows[e.RowIndex].Cells[2].Value == null) return;
        //    string tVal = "";
        //    switch (e.RowIndex)
        //    {
        //        case 0:
        //            break;
        //        case 1:
        //            break;
        //        case 2:
        //            break;
        //        case 3:
        //            break;
        //        case 4:
        //            tVal = (dataGridView_Local.Rows[e.RowIndex].Cells[2].Value as string).Trim();
        //            if (tVal != "0" && tVal != "1") break;
        //            myIO.dataSend.ResultTrig = tVal;
        //            break;

        //        case 5:
        //            tVal = (dataGridView_Local.Rows[e.RowIndex].Cells[2].Value as string).Trim();
        //            if (string.IsNullOrEmpty(tVal)) break;
        //            myIO.dataSend.ResultKind = tVal;
        //            break;

        //        default:
        //            break;
        //    }
        //}
        #endregion
    }
}
